<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
use Common\Model\PriceModel;
use Common\Model\ConfigModel;
use Common\Model\TradeSellModel;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\UserModel;
use Common\Model\LeavelModel;
use Common\Model\SplitModel;
use Common\Model\SplitLogModel;
use Common\Model\TradeBuyModel;
class PriceController extends AdminBaseController{
    protected $buy_currency;
    protected $sell_currency;
    public function __construct()
    {
        parent::__construct();
        $buy_currency_id=5;
        $sell_currency_id=1;
        
        $currency=CurrencyModel::newSelf()->getInfo();
        
        $this->buy_currency=$currency[$buy_currency_id];
        $this->sell_currency=$currency[$sell_currency_id];
        
        $this->assign('buy_currency',$currency[$buy_currency_id]);
        $this->assign('sell_currency',$currency[$sell_currency_id]);
        
    }
    public function priceLog()
    {
        $list=PriceModel::newSelf()->getPriceList();
        $this->assign('list',$list);
        $now_price=PriceModel::newSelf()->findNowPrice();
        $this->assign('now_price',$now_price);
        $this->display();
    }
    public function splitLog()
    {
        $now_price=PriceModel::newSelf()->findNowPrice();
        $this->assign('now_price',$now_price);
        $this->display();
    }
    public function changePrice()
    {
        $config=ConfigModel::newSelf()->getInfo();
        
        $price=I('price');
        
        $price_model=PriceModel::newSelf();
        
        $now_price=$price_model->findNowPrice();
        
        $this->gtValidate($price,$now_price['price'],'调整价格必须大于当前价格');

        $price_info=$price_model->findPriceByPrice($price);
        $this->notEmptyValidate($price_info,'调整价格区间必须在2-5之间');
        
        $data['sell_num']=0;
        $data['num']=0;
        $price_model->where(['id'=>['lt',$price_info['id']]])->save($data);
        
        $trade_sell_model=TradeSellModel::newSelf();
        $trade=$trade_sell_model->where(['price'=>['lt',$price_info['price']],'status'=>'0','surplus_num'=>['lt','0']])->select();
        
        $money_model=UserMoneyModel::newSelf();
        foreach ($trade as $k=>$v)
        {
            $money_model->incUserMoney($v['user_id'],$this->sell_currency['id'],$v['surplus_num']);
            $trade_sell_model->where(['id'=>$v['id']])->setField('status','-2');
        }

        $trade_buy_model=TradeBuyModel::newSelf();
        $trade_buy=$trade_buy_model->where(['price'=>['lt',$price_info['price']],'status'=>'0','surplus_num'=>['lt','0']])->select();
        $money_model=UserMoneyModel::newSelf();
        //把订单都撤掉
        foreach ($trade_buy as $k=>$v)
        {
            $money_model->incUserMoney($v['user_id'],$this->buy_currency['id'],$v['surplus_num']*$v['price']);
            $trade_buy_model->where(['id'=>$v['id']])->setField('status','-2');
        }
        
        $this->returnResult('价格调整成功',true);
    }
    public function splitPrice()
    {
        $config=ConfigModel::newSelf()->getInfo();
        
        $price=I('price');
        
        $price_model=PriceModel::newSelf();
        
        $now_price=$price_model->findNowPrice();
        
        $this->egtValidate($price,$now_price['price'],'拆分价格必须大于等于当前价格');

        $price_info=$price_model->findPriceByPrice($price);
        $this->notEmptyValidate($price_info,'调整价格区间必须在2-5之间');
        
        $data['sell_num']=0;
        $data['num']=5000;
        $price_model->where('1=1')->save($data);
        $this->backAllTradeSell();
        $this->backAllTradeBuy();

        $money_model=UserMoneyModel::newSelf();
        
        $user_model=UserModel::newSelf();
        
        $user=$user_model->where(['status'=>['neq','-1']])->select();
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $split_model=SplitModel::newSelf();
        $config=ConfigModel::newSelf()->getInfo();
        
        
        $data['price']=$price;
        $data['add_time']=time();
        $data['now_price']=$now_price['price'];
        $data['admin_id']=$_SESSION['sys_user']['userid'];
        SplitLogModel::newSelf()->add($data);
        
        //处理资金
        foreach ($user as $k=>$v)
        {
            $money=$money_model->findUserMoneyInfoByMoneyId($v['id'], $this->sell_currency['id']);

            //不能超过等级一次拆分最高额度
            $split_money=$money['num'];
            if ($split_money<=0) {
                continue;
            }
            if ($money['num']>$leavel_type[$v['leavel']]['max_split']) {
                $data1['num']=$money['num'];
                $data1['user_id']=$v['id'];
                $data1['add_time']=time();
                $data1['new_num']=$leavel_type[$v['leavel']]['max_split'];
                $data1['type']=2;
                $data1['currency']=$this->sell_currency['id'];
                $split_model->add($data1);
                $split_money=$leavel_type[$v['leavel']]['max_split'];
            }

            $data2['num']=$split_money;
            $data2['user_id']=$v['id'];
            $data2['add_time']=time();
            $end_num=$split_money/$config['BASIC_PRICE']*$price;
            $data2['new_num']=$end_num;
            $data2['currency']=$this->sell_currency['id'];
            //不能超过静态最高额度
            $money2=$money_model->findUserMoneyInfoByMoneyId($v['id'],10);
            $data2['type']=1;
            $split_model->add($data2);
            if ($end_num-$split_money>$money2['num']) {
                $data3['num']=$end_num;
                $data3['user_id']=$v['id'];
                $data3['add_time']=time();
                $data3['type']=3;
                $data3['new_num']=$money2['num']+$split_money;
                $data3['currency']=$this->sell_currency['id'];
                $split_model->add($data3);
                $end_num=$money2['num']+$split_money;
                $user_model->where(['id'=>$money['user_id']])->setField('status','-1');
            }
            $money_model->where(['id'=>$money['id']])->setField('num',$end_num);
            $this->tradeSellBegin($v['id'],$end_num*0.02,$this->buy_currency,$this->sell_currency);
            $money_model->decUserMoney($v['id'], 10,$end_num-$split_money);
        }
        $this->returnResult('拆分成功',true);
    }
    public function backAllTradeSell()
    {
        $trade_model=TradeSellModel::newSelf();
        $trade=$trade_model->where(['status'=>'0','surplus_num'=>['gt','0']])->select();
        
        
        $money_model=UserMoneyModel::newSelf();
        //把订单都撤掉
        foreach ($trade as $k=>$v)
        {
            $money_model->incUserMoney($v['user_id'],$this->sell_currency['id'],$v['surplus_num']);
            $trade_model->where(['id'=>$v['id']])->setField('status','-2');
        }
        
    }
    public function backAllTradeBuy()
    {
        $trade_model=TradeBuyModel::newSelf();
        $trade=$trade_model->where(['status'=>'0','surplus_num'=>['gt','0']])->select();
        $money_model=UserMoneyModel::newSelf();
        //把订单都撤掉
        foreach ($trade as $k=>$v)
        {
            $money_model->incUserMoney($v['user_id'],$this->buy_currency['id'],$v['surplus_num']*$v['price']);
            $trade_model->where(['id'=>$v['id']])->setField('status','-2');
        }
        
    }
    public function splitLoglist()
    {

        if ($_GET['username']) {
            $username=I('username');
            $user=$this->findUserByUserName($username);
            $where['user_id']=$user['id'];
        }
        if ($_GET['add_time']) {
            $where['add_time']=strtotime(I('add_time'));
        }
        
        $list=SplitLogModel::newSelf()->pagingList($where);
        
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->display();
    }
    public function userSplitLog()
    {
        if ($_GET['username']) {
            $username=I('username');
            $user=$this->findUserByUserName($username);
            $where['user_id']=$user['id'];
        }
        if ($_GET['add_time']) {
            $where['add_time']=strtotime(I('add_time'));
        }
        
        $list=SplitModel::newSelf()->pagingList($where);
        
        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->display();
    }
}
